package cn.xo68.boot.auth.server.web.api;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.OauthClient;
import cn.xo68.boot.auth.server.service.OauthClientService;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.StringTools;
import cn.xo68.core.util.UUIDUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/** 
 * OauthClient Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年09月10日 12:42:18
 *
 */
@RequestMapping("/api/OauthClient")
@RestController
public class OauthClientController{
    @Autowired
    private OauthClientService oauthClientService;


	@GetMapping("/find")
    public OauthClient find(@RequestParam(name="id") String id) {
        return oauthClientService.get(id);
    }


    @GetMapping("/list")
    public PageInfo<OauthClient> list(@RequestParam(name="pageIndex",defaultValue="1") int pageIndex, @RequestParam(name="pageSize",defaultValue="10") int pageSize){
        return oauthClientService.list(pageIndex, pageSize);
    }

    @RequiresPermissions("client:list")
    @GetMapping("/all")
    public List<OauthClient> listAll(){
	    return oauthClientService.list();
    }

    @RequiresPermissions("client:add")
	@PostMapping("/add")
    public String add(@RequestBody OauthClient entity ) {
        if(StringTools.isEmpty(entity.getClientId())){
            entity.setClientId(UUIDUtils.createUUId());
        }
        entity.setCreateTime(DateTime.Now().getDate());
        entity.setUpdateTime(DateTime.Now().getDate());
        entity.setClientStatus(100);
        entity.setOrderValue(100);
        oauthClientService.insert(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
    }

    @RequiresPermissions("client:delete")
	@PostMapping("/delete")
	public String delete(@RequestParam(name="id") String id) {
	    oauthClientService.delete(id);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}

    @RequiresPermissions("client:update")
	@PostMapping("/update")
	public String update(@RequestBody OauthClient entity) {
        entity.setUpdateTime(DateTime.Now().getDate());
	    oauthClientService.update(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
}